<template>
	<view class="IndustryChoicePage">
		<view class="header">
			<!-- #ifdef APP-PLUS -->
			<u-navbar title="" bgColor="transparent" titleStyle="color:#FFFFFF;font-weight:bold;" leftIconSize="36"
				leftIconColor="#FFFFFF" placeholder safeAreaInsetTop @leftClick="back">
				<u-button class="navbar-right-btn" slot="right" type="default" @click="onSure()">确定</u-button>
			</u-navbar>
			<!-- #endif -->

			<view class="content">
				<h2>您的行业及岗位是？</h2>
				<text>请谨慎选择，非会员每1年可以修改一次/会员每两 月可修改1次</text>
				<image src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/occupation/industry-head-bg.png" mode="">
				</image>
			</view>
		</view>
		<view class="search flex_center">
			<view class="search-box">
				<image src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/occupation/search.png" mode=""></image>
				<view class="line"></view>
				<u--input v-model="searchText" type="text" class="input" clearable placeholder="搜索岗位名称"
					@input="inputSearch" />
			</view>
		</view>
		<!-- 主体内容 -->
		<view class="conent-boxs">
			<view class="left">
				<view :class="item.id === activeObj.id ? 'nav-item active' : 'nav-item'"
					v-for="(item, index) in treeList" :key="index" @click="navClick(item)">
					<image class="line" src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/occupation/nav-line.png"
						mode=""></image>
					<text>{{ item.name }}</text>
				</view>
			</view>
			<view class="right" v-if="loading">
				<u-loadmore status="loading" />
			</view>
			<view class="right" v-if="!loading">
				<view class="industry-level2-item" v-for="(Item, index) in activeObj.child" :key="index">
					<view class="name">{{ Item.name }}</view>
					<view class="industry-level3-box" v-show="Item.child.length>0">
						<template v-for="(itemT, idn) in Item.child ">
							<view
								:class="[hasSelected(itemT) ? 'industry-level3-item selected' : 'industry-level3-item']"
								:key="idn" @click.stop="selectIndustry(itemT)">
								{{ itemT.name }}
							</view>

						</template>

					</view>
				</view>
			</view>
			<!-- 搜索结果展示组件 -->
			<view class="search-result" v-show="searchText">
				<view class="result-item" v-for="(item, index) in searchList" :key="index"
					@click="searchResultClick(item)">
					<view class="name" v-html="searchResultName(item.name)">{{ item.name }}</view>
					<view class="desp">{{ item.parentName }}</view>
				</view>
			</view>
		</view>
		<!-- 底部 -->
		<view class="footer">
			<text class="title">已选:</text>
			<view class="selected-list-boxs">
				<view class="selected-item" v-for="(item, index) in selectList" :key="index">
					<text>{{ item.name }}</text>
					<image @click="cancelSelect(item)"
						src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/occupation/industry-delete.png" mode="">
					</image>
				</view>
			</view>
			<!-- #ifdef H5 || MP-WEIXIN-->
			<view class="flex_center footer_button">
				<u-button class="navbar-right-btn" slot="right" type="default" @click="onSure()">确定</u-button>
			</view>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	import {
		industryTreeAll,
		industrySearch,
		memberLabel
	} from '@/apis/apis.js';
	export default {
		data() {
			return {
				loading: true,
				treeList: [],
				// 当前激活菜单
				activeObj: null,
				// 选中数据列表
				selectList: [],
				// 搜索词及搜索结果
				searchText: '',
				searchList: [{
					id: 1,
					name: '产品经理助理',
					parentName: '产品-中后台-产品经理-产品助理'
				}],

				type: 1, //1 已知首页选择行业 2兴趣
				labelPostIds:null,//选择行业类型
			};
		},
		onLoad(option) {
			this.type = option.type ? option.type : 1;
			this.selectList = option.selected ? JSON.parse(option.selected) : [];
			console.log('selectList', this.selectList);
			this.getList();
			if (this.type == 2) {
				this.memberLabel();
			}
		},
		methods: {
			memberLabel() {
				memberLabel().then(res => {
					let data = res.data;
					if(data){
						this.labelPostIds = data.labelPostIds;
					}
				})
			},
			// 获取行业岗位数据
			getList() {
				industryTreeAll().then(res => {
					if (res.code === 200) {
						this.treeList = res.data || [];
						this.activeObj = this.treeList[0];
					} else {
						uni.$u.toast(res.msg);
					}
					this.loading = false;
				});
			},
			//返回
			back() {
				uni.navigateBack();
			},
			// 点击了左侧导航item
			navClick(item) {
				this.activeObj = item;
			},
			// 判断当前数据是否已被选中
			hasSelected(item) {
				// const flag = this.selectList.includes(item)
				let flag = false;
				this.selectList.forEach(v => {
					if (v.id == item.id) {
						flag = true;
					}
					console.log(v.id, item.id);
				});
				return flag;
			},
			// 选择某一个行业标签
			selectIndustry(item) {
				console.log(item)
				if(this.labelPostIds == item.id){
					uni.$u.toast('兴趣岗位与职业体系岗位不能重复选择');
					return false;
				}
				const flag = this.hasSelected(item);
				if (flag) {
					this.cancelSelect(item);
				} else {
					this.selectList = [item];
					// if(this.selectList.length>=1){
					// 	uni.$u.toast('最多只能选择1个岗位')
					// 	return
					// }
					// this.selectList.push(item)
				}
			},
			// 从搜索结果中选择某个行业标签
			searchResultClick(item) {
				const flag = this.hasSelected(item);
				if (flag) {
					uni.$u.toast('您已经选择过了该标签');
					return;
				}
				this.selectIndustry(item);
			},
			// 取消选择某个行业标签
			cancelSelect(item) {
				this.selectList = this.selectList.filter(v => v.id !== item.id);
			},
			// 确定，返回上一个页面
			onSure() {
				console.log(this.selectList);
				// if (this.selectList.length === 0) {
				// 	uni.$u.toast('请先选择行业或岗位');
				// 	return;
				// }
				uni.$emit('industry-selected', this.selectList);
				uni.navigateBack({
					delta: 1
				});
			},
			// 搜索框输入值变化
			inputSearch(val) {
				if (!val) return;
				industrySearch({
					searchValue: val
				}).then(res => {
					console.log('result:', res);
					this.searchList = res.data || [];
					this.searchList = this.searchList.concat(this.searchList);
				});
			},
			// 正则表达式替换搜索结果中的关键词 突出显示
			searchResultName(name) {
				// 使用模板字符串 解析带有变量的正则表达式
				let reg = `/${this.searchText}/g`;
				// 使用变量 存储 字符串替换结果
				// 不修改 原始内容变量name 中存储的字符串的原始内容
				let result = ''
				// #ifdef APP-PLUS
				result = name.replace(eval(reg), `<text style="color: #194FC9;">${this.searchText}</text>`);
				// #endif
				// #ifdef H5 || MP-WEIXIN
				result = name.replace(reg, `<text style="color: #194FC9;">${this.searchText}</text>`);
				// #endif
				return result;
			}
		}
	};
</script>

<style lang="scss" scoped>
	.IndustryChoicePage {
		height: 100vh;
	}

	.header {
		height: 362rpx;
		// #ifdef APP-PLUS
		height: 440rpx;
		// #endif
		background: linear-gradient(250deg, #1f73f4, #1e8ffb, #15afff);
		color: #ffffff;
		font-size: 30rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #ffffff;
		line-height: 44rpx;

		.content {
			padding: 40rpx;
		}

		h2 {
			font-size: 52rpx;
			font-weight: bold;
			text-shadow: 0px 8px 6px rgba(101, 169, 247, 0.18);
			margin-bottom: 30rpx;
		}

		image {
			width: 355rpx;
			height: 182rpx;
			float: right;
			transform: translateY(-100rpx) translateX(40rpx);
		}
	}

	.header_r {
		padding: 10rpx 20rpx;
	}

	.navbar-right-btn {
		height: 60rpx;
		background: #ffffff;
		box-shadow: 0px 7rpx 20rpx 0px rgba(152, 152, 152, 0.16);
		border-radius: 30rpx;
		font-size: 28rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #1e88fa;
		line-height: 53rpx;
	}

	.search {
		width: 100vw;
		box-sizing: border-box;
		height: 120rpx;
		padding: 20rpx 30rpx 15rpx 30rpx;
		margin-top: -30rpx;
		// #ifdef APP-PLUS
		// margin-top: 20rpx;
		// #endif
		background: #ffffff;
		box-sizing: border-box;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		.search-box {
			display: flex;
			height: 75rpx;
			width: 100%;
			background: rgba(243, 243, 243, 0.1);
			border: 1px solid #cecece;
			border-radius: 38rpx;
			// #ifdef APP-PLUS
			// transform: translateY(-60rpx);
			// #endif
			box-sizing: border-box;
			padding: 16rpx 20rpx;
		}

		image {
			width: 40rpx;
			height: 40rpx;
			vertical-align: middle;
			margin-right: 10rpx;
		}

		.input {
			height: 75rpx;
			margin: 0;
			margin-top: -20rpx;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #adadad;
			line-height: 53rpx;
			border-radius: 0 38rpx 38rpx 0;
			border: none;
		}

		.line {
			width: 4rpx;
			background: #207ff2;
		}
	}

	.conent-boxs {
		display: flex;
		position: relative;

		.left {
			width: 246rpx;
			height: calc(100vh - 560rpx);
			background: #f5f6f8;
padding-bottom: env(safe-area-inset-bottom);
			.nav-item {
				height: 115rpx;
				line-height: 115rpx;
				padding-left: 30rpx;
				font-size: 30rpx;
				font-family: Source Han Sans CN;
				font-weight: 500;
				color: #1f1f1f;

				.line {
					display: none;
				}
			}

			.active {
				background: #ffffff;
				position: relative;
				border-radius: -12rpx;

				.line {
					width: 8rpx;
					height: 34rpx;
					display: inline-block;
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
				}

				&::after {
					content: '';
					width: 34rpx;
					height: 151rpx;
					position: absolute;
					right: -8rpx;
					z-index: 99;
					background-image: url('https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/occupation/nav-item-right-bg.png');
					background-size: 100% 100%;
					background-repeat: no-repeat;
					transform: translateY(-20rpx);
				}
			}
		}

		//
		.right {
			margin: 0 30rpx;
			height: calc(100vh - 560rpx);
			flex: 1;
			overflow-y: auto;
			overflow-x: hidden;
			padding-bottom: env(safe-area-inset-bottom);
			.industry-level2-item {
				.name {
					font-size: 34rpx;
					font-family: Source Han Sans CN;
					font-weight: 500;
					color: #303030;
					line-height: 53rpx;
					margin-bottom: 20rpx;
				}

				.industry-level3-box {
					width: 450rpx;
					display: flex;
					flex-wrap: wrap;
					margin-bottom: 50rpx;
					position: relative;

					.industry-level3-item {
						width: 200rpx;
						height: 70rpx;
						background: #f5f6f8;
						border: 2rpx solid #f5f6f8;
						border-radius: 10rpx;
						text-align: center;
						line-height: 70rpx;
						font-size: 28rpx;
						font-family: Source Han Sans CN;
						font-weight: 400;
						color: #7b7b7b;
						margin-bottom: 20rpx;

						&:nth-child(2n + 1) {
							margin-right: 22rpx;
						}
					}

					.industry-level3-item.selected {
						background: #edf6ff;
						border: 2px solid #7cb7ff;
						color: #4696f8;
					}

					&:empty::after {
						content: '没有数据';
						position: absolute;
						left: 40%;
						font-size: 22rpx;
						top: -20rpx;
						color: #7b7b7b;
					}
				}
			}
		}
	}

	.footer {
		width: 100%;
		background: #ffffff;
		box-shadow: 0px -2rpx 20rpx 0px rgba(152, 152, 152, 0.12);
		position: fixed;
		bottom: 0;
		padding: 20rpx 30rpx calc(env(safe-area-inset-bottom) + 20rpx) 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.title {
			display: inline-block;
			height: 50rpx;
			line-height: 50rpx;
		}

		.selected-list-boxs {
			flex: 1;
			height: 52rpx;
			margin-left: 10rpx;
			line-height: 52rpx;
			display: flex;
			overflow-x: auto;

			.selected-item {
				padding: 0 16rpx;
				height: 50rpx;
				background: #edf6ff;
				border-radius: 25rpx;
				box-sizing: border-box;
				font-size: 26rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #4696f8;
				line-height: 50rpx;
				margin-right: 12rpx;

				image {
					width: 18rpx;
					height: 18rpx;
					margin-left: 12rpx;
				}
			}
		}

		.footer_button {
			position: absolute;
			right: 20rpx;
			top: 20rpx;
		}
	}

	// 搜索结果展示部分
	.search-result {
		width: 100vw;
		height: 100%;
		// #ifdef APP-PLUS
		height: calc(100% - 80rpx);
		// #endif
		position: absolute;
		top: 0;
		left: 0;
		background: #ffffff;
		padding: 0 40rpx;
		z-index: 999;
		overflow-y: auto;

		.result-item {
			padding: 30rpx 0;
			border-bottom: 1rpx solid #e5e5e5;
			font-family: Source Han Sans CN;
			line-height: 46rpx;
			font-weight: 600;
			letter-spacing: 0.05em;

			.name {
				font-size: 32rpx;
				margin-bottom: 10rpx;
			}

			.desp {
				font-size: 28rpx;
				font-weight: 400;
				color: #7b7b7b;
			}
		}
	}
</style>
